package com.cheer.ba.view.activity;

import android.content.Intent;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.andview.refreshview.XRefreshView;
import com.andview.refreshview.footview.CustomerFooter;
import com.andview.refreshview.smileyloadingview.SmileyHeaderView;
import com.bigkoo.svprogresshud.SVProgressHUD;
import com.cheer.ba.R;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.event.BaseEvent;
import com.cheer.ba.model.OilCardInfoModel;
import com.cheer.ba.presenter.impl.OilMealPresenter;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.utils.ToastUtil;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.view.base.CommonAdapter;
import com.cheer.ba.view.base.ViewHolder;
import com.cheer.ba.widget.observable.ObservableListView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 搜索司机
 */
public class SearchDriversAty extends BaseCommAty {

    @BindView(R.id.id_search_drivers_xrefreshv)
    XRefreshView mXRefreshView;
    @BindView(R.id.id_search_drivers_listv)
    ObservableListView mObserListv;
    CommonAdapter<OilCardInfoModel> mAdapter;
    List<OilCardInfoModel> mDriverLists = new ArrayList<OilCardInfoModel>();

    //柴油卡号
    private String mCardNum;

    //搜索关键字
    private EditText mEditKey;
    private String mSaveKey;

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_search_driver, null, false));
    }

    @Override
    public void initPresenter() {
        presenter = new OilMealPresenter();
    }

    @Override
    public boolean hasDataInPage() {
        return !mDriverLists.isEmpty();
    }

    @Override
    public void initView() {
        mCardNum = getIntent().getStringExtra(IntentKey.General.KEY_DATA);
        mEditKey = setSearchTxt("", null);
        setTitleRight("搜索", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSaveKey = mEditKey.getText().toString().trim();
                if (StringUtils.isEmpty(mSaveKey)) {
                    SVProgressHUD.showInfoWithStatus(aty, "请输入关键字");
                } else {
                    //搜索
                    loadData(true, false);
                }
            }
        });
    }

    @Override
    public void initData() {
        setXRefreshView(mXRefreshView, true, true, false, true, new SmileyHeaderView(aty), new CustomerFooter(aty));
        mAdapter = new CommonAdapter<OilCardInfoModel>(aty, mDriverLists, R.layout.adp_driver_item) {
            @Override
            public void convert(ViewHolder helper, final OilCardInfoModel model) {
                if (null != model) {
                    if (StringUtils.isEmpty(model.getPlateNumber())) {
                        helper.setText(R.id.id_driver_car_num_txt, "车牌号:未知");
                    } else {
                        helper.setText(R.id.id_driver_car_num_txt, "车牌号:" + model.getPlateNumber());
                    }
                    if (StringUtils.isEmpty(model.getCardNum())) {
                        helper.setText(R.id.id_driver_oil_num_txt, "卡号: 未关联");
                        helper.setText(R.id.id_driver_change_txt, "关联油卡");
                    } else {
                        helper.setText(R.id.id_driver_oil_num_txt, "卡号: " + model.getCardNum());
                        helper.setText(R.id.id_driver_change_txt, "查看流水");
                    }
                    helper.setText(R.id.id_driver_name_txt, model.getName());
                    helper.setText(R.id.id_driver_tel_txt, model.getTel());

                    helper.getViewById(R.id.id_driver_change_txt).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //去关联
                            if (!StringUtils.isEmpty(mCardNum)) {
                                ((OilMealPresenter) presenter).bindDriverInfo(model.getUid(), mCardNum);
                            } else {
                                Intent card = new Intent(aty, OilCardAty.class);
                                card.putExtra(IntentKey.General.KEY_PAGE, true);
                                showActivity(aty, card);
                            }
                        }
                    });
                }
            }
        };
        mObserListv.setAdapter(mAdapter);
        mXRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener() {
            @Override
            public void onRefresh(final boolean isPullDown) {
                loadData(false, false);
            }

            @Override
            public void onLoadMore(boolean isSilence) {
                loadData(false, true);
            }
        });
    }

    /**
     * 加载数据
     */
    public void loadData(boolean isShLowoad, boolean loadMore) {
        ((OilMealPresenter) presenter).getDriverList(isShLowoad, loadMore, "", "", "", "", mSaveKey);
        if (!isShLowoad) {
            if (loadMore) {
                if (hasDataInPage()) {
                    stopLoadComplete(mXRefreshView, 2000);
                } else {
                    stopLoadMore(mXRefreshView, 2000);
                }
            } else {
                stopRefresh(mXRefreshView, 2000);
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventUpdate(BaseEvent event) {
        //返回类型
        String type = event.type;
        if (TextUtils.equals(type, IntentKey.Oil.KEY_OIL_MODEL)) {
            OilCardInfoModel model = (OilCardInfoModel) event.model;
            if (null != model) {
                ((OilMealPresenter) presenter).bindDriverInfo(model.getUid(), model.getCardNum());
            }
        }
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.BIND_DRIVERS)) {
                //绑定司机成功
                OilCardInfoModel model = (OilCardInfoModel) t;
                if (null != model) {
                    SVProgressHUD.showSuccessWithStatus(aty, "绑定成功");
                    BaseEvent event = new BaseEvent();
                    event.type = ConstantTag.BIND_DRIVERS;
                    event.model = model;
                    EventBus.getDefault().post(event);
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            finish();
                        }
                    }, 2000);
                }
            } else if (TextUtils.equals(tag, ConstantTag.DEL_DRIVERS)) {
                //移除成功
                mXRefreshView.startRefresh();
                OilCardInfoModel model = (OilCardInfoModel) t;
                BaseEvent event = new BaseEvent();
                event.type = ConstantTag.DEL_DRIVERS;
                event.model = model;
                EventBus.getDefault().post(event);
            }
        }
    }

    @Override
    public <T> void updateViewWithLoadMore(T t, boolean loadMore, String tag) {
        if (loadMore) {
            if (hasDataInPage()) {
                mXRefreshView.setLoadComplete(true);
            } else {
                mXRefreshView.stopLoadMore();
            }
        } else {
            mXRefreshView.stopRefresh();
        }
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.GET_DRIVERS_LIST)) {
                //获得司机列表
                List<OilCardInfoModel> mDrivers = (List<OilCardInfoModel>) t;
                if (!loadMore) {
                    mDriverLists.clear();
                }
                if (mDrivers.isEmpty()) {
                    if (loadMore) {
                        ToastUtil.showToast(getString(R.string.no_more_data));
                    } else {
                        dataStatus(3);
                    }
                } else {
                    mDriverLists.addAll(mDrivers);
                    mAdapter.notifyDataSetChanged();
                }
            }
        }
    }
}
